#include "parse.h"

void init_parser( ) {
    init_log( );
    path = malloc( ( 18 + strlen( PREFIX ) ) * sizeof( char ) );
    path = strcpy( path, PREFIX ) ;
    path = strcat( path, "etc/urlstorage.xml" );
    
    xml_init_storage( path );
    
    uris[ 0] = "http://";
    uris[ 1] = "https://";
    uris[ 2] = "mailto:";
    uris[ 3] = "feed://";
    uris[ 4] = "rss://";
    uris[ 5] = "ftp://";
    uris[ 6] = "sftp://";
    uris[ 7] = "see://";
    uris[ 8] = "afp://";
    uris[ 9] = "smb://";
    uris[10] = "spotify://";
    uris[11] = "irc://";
    uris[12] = "ssh://";
    uris[13] = "jabber://";
    uris[14] = "xmpp://";
    uris[15] = "vnc://";
    uris[16] = "itms://";
    uris[17] = "file:///";
    uris[18] = "shortit://";
}

void parse( char *in, char *out ) {
    if( str_has_prefx( in, "GET /compress?q=" ) )
        send_compress( in, out );
    else if( str_has_prefx( in, "GET /..adminreset.. " ) )
        adminreset( in, out );
    else if( str_has_prefx( in, "GET / " ) )
        send_main_page( out );
    else if( str_has_prefx( in, "GET /" ) )
        send_expand( in, out );
    else
        send_error( in, out );
}

void get_addr( char *request, char *addr, int n ) {
    int i;
    int max;
    if( n == 16 ) // compress
        max= 2048;
    else if( n == 5 ) //expand
        max= 32;
    for( i =n; request[ i ] != ' ' ; i++ )
        addr[ i- n] = request[ i ];
    addr[i - n] = '\0';
}


void send_compress( char *in, char *out ) {
    char addr[2048];
    get_addr( in, addr, COMPRESS_NUM );
    char* id = xml_compress_new( addr );
  printf( "send_compress( ): %s", id );
    if( id==NULL )
    sprintf( out, "%sAn error has occurred. Maybe I'll tell you more, a bit later ;)", http_head );
    sprintf( out, "%s%s%s\r\n\0", http_head, HOST_ADDR, id );
    
    #ifdef WANT_LOG
        char logstr[ strlen(id) + 9 ];
        sprintf( logstr, "compressed %s to %s", addr, id );
        log( STD, logstr );
    #endif
}

void send_main_page( char *out ) {
    #ifdef WANT_LOG
        log( STD, "user requested main page." );
    #endif
    strcpy( out, "HTTP/1.1 200 OK\r\nConnection: close\r\nContent-type: text/html; charset=UTF-8\r\n\r\n<head><frameset><frame src=\"http://frocensoftware.bplaced.net/ctu/webinterface/\" width=100% height=100% /></frameset></head>" );
}

void send_expand( char *in, char *out ) {
    char id[ 32];
    get_addr( in, id, EXPAND_NUM );
    enum { REDIRECT, PREVIEW } action = REDIRECT;
    
    // does the user want a preview?
    if( str_has_suffx( id, "?" ) ) {
        id[strlen( id)-1 ] = 0;
        action = PREVIEW;
    }
    
    char *addr = xml_get_url( id );
    
    if( addr == NULL ) {
        strcpy( out, errhead"<h1>Error:</h1>The id, you requested was not found in our database. Please check your link to the page or try another one." );
        return;
    }
    
    if( action == PREVIEW ) {
        sprintf( out, http_head"<a href=\"%s\">%s</a>", addr, addr );
        #ifdef WANT_LOG
        {
            char logstr[ strlen(id) + 14 + strlen ( addr)];
            sprintf( logstr, "previewed %s to %s", id, addr );
            log( STD, logstr );
        }
        #endif
        return;
    }
    
    sprintf( out, "%s%s\r\n\r\n\0", redirect_head, addr );
    #ifdef WANT_LOG
    {
        char logstr[ strlen(id) + 13 + strlen( addr ) ];
        sprintf( logstr, "expanded %s to %s", id, addr );
        log( STD, logstr );
    }
    #endif
    return;
}

void send_error( char *in, char *out ) {
    strcpy( out, "HTTP/1.1 200 OK\r\nConnection: close\r\nContent-type: text/html; charset=UTF-8\r\n\r\nError, file not found." );
}

void adminreset( char *in, char *out ) {
    xml_init_storage( path );
    strcpy( out, http_head "Succesfully reset the database." ); 
}

